###############################################################################
# Makefile for the project Moussavi_1V7
###############################################################################

## General Flags
PROJECT = Moussavi_1V7
MCU = at90usb647
TARGET = Moussavi_1V7.elf
CC = avr-gcc

CPP = avr-g++

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2 -std=gnu99      -DF_CLOCK=16000000UL    -DUSB_DEVICE_ONLY  -D FIXED_CONTROL_ENDPOINT_SIZE=8  -D FIXED_NUM_CONFIGURATIONS=1  -D USE_FLASH_DESCRIPTORS  -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"    -DF_CPU=16000000UL -O3 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=Moussavi_1V7.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Include Directories
INCLUDES = -I"C:\Projects\Moussavi Project\EmbeddedApplication\Moussavi_1.7\Moussavi_1.7\." -I"C:\Projects\Moussavi Project\EmbeddedApplication\Moussavi_1.7\Moussavi_1.7\FreakUSB\usb" -I"C:\Projects\Moussavi Project\EmbeddedApplication\Moussavi_1.7\Moussavi_1.7\FreakUSB\class\CDC" -I"C:\Projects\Moussavi Project\EmbeddedApplication\Moussavi_1.7\Moussavi_1.7\FreakUSB\hw\at90usbxx6_7" 

## Objects that must be built in order to link
OBJECTS = DataFlash.o Moussavi_1.7.o cdc.o desc.o ep.o hw.o isr.o ctrl.o usb.o usb_buf.o Moussavi_transfer_protocol.o Moussavi_file_system.o Moussavi_Processing.o 

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) Moussavi_1V7.hex Moussavi_1V7.eep Moussavi_1V7.lss size

## Compile
DataFlash.o: ../DataFlash.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Moussavi_1.7.o: ../Moussavi_1.7.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

cdc.o: ../FreakUSB/class/CDC/cdc.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

desc.o: ../FreakUSB/class/CDC/desc.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ep.o: ../FreakUSB/hw/at90usbxx6_7/ep.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

hw.o: ../FreakUSB/hw/at90usbxx6_7/hw.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

isr.o: ../FreakUSB/hw/at90usbxx6_7/isr.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

ctrl.o: ../FreakUSB/usb/ctrl.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

usb.o: ../FreakUSB/usb/usb.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

usb_buf.o: ../FreakUSB/usb/usb_buf.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Moussavi_transfer_protocol.o: ../Moussavi_transfer_protocol.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Moussavi_file_system.o: ../Moussavi_file_system.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

Moussavi_Processing.o: ../Moussavi_Processing.S
	$(CC) $(INCLUDES) $(ASMFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
	 $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump -h -S $< > $@

size: ${TARGET}
	@echo
	@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
	-rm -rf $(OBJECTS) Moussavi_1V7.elf dep/* Moussavi_1V7.hex Moussavi_1V7.eep Moussavi_1V7.lss Moussavi_1V7.map


## Other dependencies
-include $(shell mkdir dep 2>NUL) $(wildcard dep/*)

